package pl.bristleback.server.bristle.binding.resolvers;

import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.utils.StringUtil;

import java.math.BigDecimal;

/**
 * Creates a new BigDecimal instance using various constructors depending on the parameter type.
 * Every comma is replaced by dot.
 * <p/>
 * Created on: 2011-04-03 11:00:17 <br/>
 *
 * @author Wojciech Niemiec
 */
public class BigDecimalPropertyResolver implements PropertyValueResolver<BigDecimal> {
  private static Logger log = Logger.getLogger(BigDecimalPropertyResolver.class.getName());

  public BigDecimal extractValue(Object value) {
    if (value instanceof Double) {
      return new BigDecimal((Double) value);
    } else if (value instanceof Integer) {
      return new BigDecimal((Integer) value);
    } else if (value instanceof Long) {
      return new BigDecimal((Long) value);
    } else if (value instanceof String) {
      String processedValue = ((String) value).replace(StringUtil.COMMA, StringUtil.DOT);
      try {
        return new BigDecimal(processedValue);
      } catch (NumberFormatException e) {
        return null;
      }
    }
    return null;
  }
}
